# -*- coding: utf-8 -*-

#   This file is part of emesene.
#
#    Emesene is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    emesene is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with emesene; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

VERSION = '0.1'

import Plugin
import xmlrpclib

ERROR = ''

class MainClass( Plugin.Plugin ):
    '''Main plugin class'''
    
    description = _( 'Adds a /wp command to post in wordpress blogs' )
    authors = { 'Roger Duran' : 'RogerDuran at gmail dot com' }
    website = 'http://www.rogerpc.com.ar'
    displayName = _( 'Wordpress' )
    name = 'Wordpress'
    def __init__( self, controller, msn ):
        '''Contructor'''
        
        Plugin.Plugin.__init__( self, controller, msn )
        
        self.description = _( 'Adds a /wp command to post in wordpress blogs' )
        self.authors = { 'Roger Duran' : 'RogerDuran at gmail dot com' }
        self.website = 'http://www.rogerpc.com.ar'
        self.displayName = _( 'Wordpress' )
        self.name = 'Wordpress'

        self.config = controller.config
        self.config.readPluginConfig(self.name)

        self.server = None
        self.url = self.config.getPluginValue( self.name, 'url', '' )
        self.user = self.config.getPluginValue( self.name, 'user', '' )
        self.password = self.config.getPluginValue( self.name, 'password', '' )
        
        self.Slash = controller.Slash
        
        self.controller = controller

    def start( self ):
        '''start the plugin'''
        self.Slash.register('wp', self.slashCommand, 'Wordpress publisher')
        if self.url != '':
            self.server = xmlrpclib.Server(self.url)
        self.enabled = True
        
    def stop( self ):    
        '''stop the plugin'''
        self.Slash.unregister('wp')
        self.enabled = False
        
    def slashCommand( self, slashAction ):
        if self.user == '' or self.password == '':
            slashAction.outputText(_('Configure the plugin'))
            return
        post = slashAction.getParams().split('\n',1)
        if len(post)<2:
            slashAction.outputText(_('Insert Title and Post'))
            return
        
        slashAction.outputText('Title:·#'+post[0]+'·#\nPost:\n'+post[1])
        post={'title':post[0], 'description':post[1]}
        
        postId = int(self.server.metaWeblog.newPost(0, self.user, self.password, post, 1))

        
    def action( self ):
        pass
    
    def check( self ):
        '''
        check if everything is OK to start the plugin
        return a tuple whith a boolean and a message
        if OK -> ( True , 'some message' )
        else -> ( False , 'error message' )
        '''
        if ERROR != '':
            return ( False, ERROR )
            
        return ( True, 'Ok' )
        
    def configure( self ):
        l=[]
        l.append( Plugin.Option( 'url', str, _('Url of WP xmlrpc:'), '', self.config.getPluginValue( self.name, 'url', '' )) )
        l.append( Plugin.Option( 'user', str, _('User:'), '', self.config.getPluginValue( self.name, 'user', '' )) )
        l.append( Plugin.Option( 'password', str, _('Password:'), '', self.config.getPluginValue( self.name, 'password', '')) )
        
        response = Plugin.ConfigWindow( _( 'Wordpress plugin config' ), l ).run()
        if response != None:
            self.url = response['url'].value
            self.user = response['user'].value
            self.password = response['password'].value
            self.server = xmlrpclib.Server(self.url)

            self.config.setPluginValue( self.name,'url', self.url )
            self.config.setPluginValue( self.name,'user',self.user )
            self.config.setPluginValue( self.name, 'password',self.password )
        return True
